import { loginByWx } from '@/api/user.js'

const getToken = () => {
  return uni.getStorageSync('token')
}

const user ={
  // namespaced: true,
  
  state: {
    token: getToken()
  },
  
  mutations: {
    SET_TOKEN:(state, token) => {
      console.log('setting token')
      uni.setStorageSync('token', token)
      state.token = token
    }
  },
  
  actions: {
    logOut: ({commit}) => {
      commit('SET_TOKEN', '')
    },
    loginByWxReq:({commit}) => {
      uni.login({
        success: (res) => {
          const data = {
            code: res.code
          }
          loginByWx(data)
            .then(res => {
              console.log(res)
              uni.setStorageSync('token',res.data.token)
              commit('SET_TOKEN', res.data.token)
            })
            .catch()
        }
      })
    }
  }
}

export default user